<?php

namespace App\Http\Controllers\back;


use App\Model\readuser;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\DB;

class ReaduserController extends Controller
{
    //用户信息管理
    public function news()
    {
//        $news = readuser::all();
        $news = DB::table('readusers')->paginate(4);
        return view('back.readuser.news', compact('news'));
    }

    //添加用户
    public function adduser(Request $request)
    {

        if ($request->isMethod('post')) {

            $add = new readuser();
            $add->nickname = $request->name;
            $add->tel = $request->tel;
            $add->email = $request->email;
            $add->sex = $request->sex;
            $add->pwd = $request->password;
            $add->regtime = time();
            $add->save();
            $id = $add->id;
            if ($request->pic) {

                $name = $id . '.jpg';
                $dir = public_path() . '\image\readuser';

                $request->pic->move(public_path() . '/image/readuser', $name);
                $upnew = readuser::find($id);
                $upnew->icon = $name;
                $upnew->save();
            }

            return redirect('back/read/news');
        }
        return view('back.readuser.addreaduser');
    }

    //更新用户
    public function upreaduser(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $up = readuser::find($id);
            $up->nickname = $request->nickname;
            $up->tel = $request->tel;
            $up->sex = $request->sex;
            $up->email = $request->email;
            $up->save();
            return redirect('back/read/news');
        }
        $jg = readuser::find($id);
        return view('back.readuser.upreaduser', compact('jg'));
    }

    //删除用户
    public function delreaduser($id)
    {

        $del = readuser::find($id);
        $del->delete();
        return redirect('back/read/news');
    }

    //订单管理
    public function purchase()
    {
//        $name = $this->belongsTo

        $order=DB::table('orders')->join('readusers','orders.readid','readusers.id')->paginate(4);
//            dd($order);
        return view('back.readuser.purchase',compact('order'));

    }

    //查看个人订单
    public function statuser($id)
    {
        $result=DB::table('orders')->join('books','books.id','orders.bid')->join('readusers','orders.readid','readusers.id')->where('readusers.id',$id)->orderby('orders.addtime')->get();
//        $result = order::where('readid',$id)->get();
//        dd($result);
        return view('back.readuser.statuser',compact('result'));
    }

    //所有充值记录
    public function gomoney()
    {
        $gomoney=DB::table('gomoneys')->join('readusers','gomoneys.readid','readusers.id')->paginate(4);

        return view('back.readuser.gomoney',compact('gomoney'));
    }


}
